package com.mhs.services;


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.mhs.DTO.UserDTO;

public class ServiceSearch {
	private static final String SOAP_ACTION = "http://tempuri.org/timKiemThuoc";
	
	private static final String OPERATION_NAME = "timKiemThuoc";
	
	private static final String WSDL_TARGET_NAMESPACE = PublicVariable.getWsdlTargetNamespace();
	
	private static final String SOAP_ADDRESS = PublicVariable.getSoapAddress();
	
	public static SoapObject InvokeMethod(UserDTO user, String keyworld, String type){
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
		
		if(!keyworld.isEmpty() && !type.isEmpty() && user != null){
			PropertyInfo pi = new PropertyInfo();
	        pi.setName("USER");
	        pi.setValue(user);
	        pi.setType(UserDTO.class);
	        request.addProperty(pi);
	        
			PropertyInfo pi1 = new PropertyInfo();
	        pi1.setName("keyword");
	        pi1.setValue(keyworld);
	        pi1.setType(String.class);
	        request.addProperty(pi1);
	        
	        PropertyInfo pi2 = new PropertyInfo();
	        pi2.setName("type");
	        pi2.setValue(type);
	        pi2.setType(String.class);
	        request.addProperty(pi2);
		}
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		envelope.addMapping(WSDL_TARGET_NAMESPACE, "UserDTO",new UserDTO().getClass());	
		HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_ADDRESS);
		SoapObject response = null;
	    try{
	    	androidHttpTransport.call(SOAP_ACTION, envelope);
	    	//SoapObject response = (SoapObject)envelope.getResponse();
	    	//String result = response.getProperty(0).toString();
	    	response = (SoapObject) envelope.getResponse();
	    }catch (Exception exception){
	    	exception.printStackTrace();
	    }
		return response;
	}
}
